//
//  MXHomeManageView.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/11.
//

#import "MXHomeManageView.h"
#import "MXHomeModel.h"
@implementation MXHomeManageView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    
    MJWeakSelf
    self.width = kScreenW - 32;
    self.height = 96;
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    self.userInteractionEnabled = YES;
    self.backgroundColor = [UIColor whiteColor];
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW - 32, 48)];
    [self addSubview: self.topView];
    self.topView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
//    [self.topView dc_cornerRadius:10 rectCorner:UIRectCornerTopLeft | UIRectCornerTopRight];
    
   self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 48, kScreenW - 32, 48)];
    self.bottomView.userInteractionEnabled = YES;
    [self addSubview:self.bottomView];
    self.bottomView.backgroundColor = [UIColor whiteColor];
//    [self.bottomView dc_cornerRadius:10 rectCorner:UIRectCornerBottomRight | UIRectCornerBottomLeft];
    [self.bottomView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.manageClick) {
            weakSelf.manageClick();
        }
    }]];
    
    UILabel *manageLab = [[UILabel alloc] init];
    [self.bottomView addSubview:manageLab];
    manageLab.text = @"家庭管理";
    manageLab.textColor = [UIColor colorWithHexString:@"#333333"];
    manageLab.font = PFRFontMedium(14);
    
    [manageLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bottomView);
        make.left.offset(40);
    }];
    
    UIImageView *setImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_set"]];
    [self.bottomView addSubview:setImageV];
    [setImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bottomView);
        make.height.width.mas_equalTo(24);
        make.right.offset(-13);
    }];
}

- (void)realodViewWithHomeArray:(NSArray *)homeArray withId:(nonnull NSString *)currentId {
    self.dataArray = homeArray;
    for (UIView*view in self.topView.subviews) {
        [view removeFromSuperview];
    }
    self.topView.frame = CGRectMake(0, 0, kScreenW-32, 48*homeArray.count);
    self.bottomView.frame = CGRectMake(0, 48*homeArray.count, kScreenW-32, 48);
    self.height = 48+ 48*homeArray.count;
    for (int i=0; i<homeArray.count; i++)
    {
        MXHomeModel * model = homeArray[i];
        UIButton*btn = [[UIButton alloc] init];
        btn.titleLabel.font = PFRFontMedium(14);
        btn.frame = CGRectMake(10, 48*i, kScreenW-52, 48);
        [btn setTitle:[NSString stringWithFormat:@"  %@",model.familyName] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"#3278F6"] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"duihao"] forState:UIControlStateSelected];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        if ([currentId isEqualToString:model.familyId])
        {
            btn.selected = YES;
        }
        else{
            btn.selected = NO;
        }
        btn.tag = i;
        [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        [self.topView addSubview:btn];
    }
}

- (void)btnclick:(UIButton*)btn{
    MXHomeModel*model = self.dataArray[btn.tag];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"enterhome" object:model.mj_JSONObject];
    if (self.dismissClick) {
        self.dismissClick();
    }
}
@end
